< previous page page_276 next page >

Page 276
Opening a Database
The first step in writing most database applications is to connect or open the database you'll be working with. Opening a database is a lot like opening a file, except that you're using the ADO commands instead of the simple Basic Open statement. When you open a database, you're actually creating a Database object that will be used by the other Data Access Objects. To access the ADO command set, you must include in your project a reference for Microsoft ActiveX Data Object Library, which contains all the ADO command objects.
The database is represented in the application by the ADO Connection object. A working database session is defined by creating RecordSet objects within the Connection object. You can then open a query with the RecordSet object's Open method. To use the Open method, create a recordset object and call the method, as follows:
Dim cnn1 As ADODB.Connection
Dim rstEmployees As ADODB.Recordset
Dim strCnn As String
Dim varDate As Variant
 Open connection.
strCnn = driver={SQL Server} ; server=srv; & _
      uid=sa;pwd=;database=pubs
Set cnn1 = New ADODB.Connection
cnn1.Open strCnn
After you open the connection, only a link from your application to the database has been created. You still can't access the data in the database. To access the data, you need to create and open a Recordset object that links to the data stored in the database. When creating a Recordset object in your application, you can access an entire table, specific fields and records from a table, or a specify combination of records and fields from several tables. You can open two different types of recordsets in your application by setting the Recordsets Options parameter to one of the following values listed in Table 10.3.
TABLE 10.3. THE RECORDSET OPTIONS SETTINGS.
OptionDescription
adCmdTextIndicates that a SQL statement is in the Source parameter
adCmdTableSpecifies that the ADO should create a SQL query to return the table referenced in the Source parameter

Each type has its own advantages and disadvantages when using them. Take a closer look at each to see how they work.

 
< previous page page_276 next page >

If you like this book, buy it!